package sg.edu.nus.iss.phoenix.radioprogram.service;

import sg.edu.nus.iss.phoenix.core.dao.DAOFactoryImpl;
import sg.edu.nus.iss.phoenix.core.exceptions.NotFoundException;
import sg.edu.nus.iss.phoenix.radioprogram.dao.RadioProgramDAO;
import sg.edu.nus.iss.phoenix.radioprogram.entity.RadioProgram;

import java.sql.SQLException;
import java.util.ArrayList;

public class RadioProgramService {
    DAOFactoryImpl factory;
    RadioProgramDAO rpdao;

    /**
     * constructor
     */
    public RadioProgramService() {
        super();
        // TODO Auto-generated constructor stub
        factory = new DAOFactoryImpl();
        rpdao = factory.getRadioProgramDAO();
    }

    /**
     * constructor
     *
     * @param radioMockDao
     */
    public RadioProgramService(RadioProgramDAO radioMockDao) {
        rpdao = radioMockDao;
    }

    /**
     * search all programs that matched the search criteria
     *
     * @param rpso
     * @return radio program list
     */
    public ArrayList<RadioProgram> searchPrograms(RadioProgram rpso) {
        ArrayList<RadioProgram> list = new ArrayList<RadioProgram>();
        try {
            list = (ArrayList<RadioProgram>) rpdao.searchMatching(rpso);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return list;
    }

    /**
     * search all programs that matched the search criteria
     *
     * @param rp
     * @return radio program list
     */
    public ArrayList<RadioProgram> findRPByCriteria(RadioProgram rp) {
        ArrayList<RadioProgram> currentList = new ArrayList<RadioProgram>();

        try {
            currentList = (ArrayList<RadioProgram>) rpdao.searchMatching(rp);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return currentList;

    }

    /**
     * find radio program by name
     *
     * @param rpName
     * @return radio program object
     */
    public RadioProgram findRP(String rpName) {
        RadioProgram currentrp = new RadioProgram();
        currentrp.setName(rpName);
        try {
            currentrp = ((ArrayList<RadioProgram>) rpdao.searchMatching(currentrp)).get(0);
            return currentrp;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return currentrp;

    }

    /**
     * get all radio programs
     *
     * @return list of programs
     */
    public ArrayList<RadioProgram> findAllRP() {
        ArrayList<RadioProgram> currentList = new ArrayList<RadioProgram>();
        try {
            currentList = (ArrayList<RadioProgram>) rpdao.loadAll();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return currentList;

    }

    /**
     * insert radio program
     *
     * @param rp
     */
    public void insertRP(RadioProgram rp) {
        try {
            rpdao.create(rp);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * update radio program
     *
     * @param rp
     */
    public void updateRP(RadioProgram rp) {

        try {
            rpdao.save(rp);
        } catch (NotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * delete radio program
     *
     * @param rp
     */
    public void deleteRP(RadioProgram rp) {

        try {
            rpdao.delete(rp);
        } catch (NotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
